package cn.itsource.controller;

import cn.itsource.constant.SystemConstant;
import cn.itsource.domain.User;
import cn.itsource.exception.BusinessException;
import cn.itsource.service.UserService;
import cn.itsource.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * 接口类：系统
 */
@RestController
public class SystemController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param map
     * @param session
     * @return
     */
    //接收参数：1.参数名 2.对象【自定义对象，Api自带的对象Map】 3.路径表达式
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String,String> map, HttpSession session){
        try {
            User user = userService.login(map);
            //将登录信息放入session【自动创建session对象，而且分配一个相应id】
            session.setAttribute(SystemConstant.USER_IN_SESSION,user); //做登录拦截
            return AjaxResult.me().setObj(user);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，稍后重试");
        }
    }

}
